# -*- Mode: perl; -*-
# Add a "LIBADD" action.  

$KnownActions[$#KnownActions+1] = "LIBADD";

%lib_addobjects = ();
%liblt_addobjects = ();
%libso_addobjects = ();

sub ClearLIBADD {
    %lib_addobjects = ();
    %liblt_addobjects = ();
    %libso_addobjects = ();
}

sub ActionLIBADD {
    my $line = $_[0];

    print "Invoking action LIBADD\n";

    if ($line =~ /^lib([@\${}\(\)\w-]*)_([ls]?[oa]+)_LIBADD\s*=\s*(.*)\s*$/) {
	my $libname = $1;
	my $libtype = $2;
	my $objects = $3;
	if ($libtype eq "lt") {
	    $liblt_addobjects{$libname} = $objects;
	}
	elsif ($libtype eq "a") {
	    $lib_addobjects{$libname} = $objects;
	}
	elsif ($libtype eq "so") {
	    $libso_addobjects{$libname} = $objects;
	}
	else {
	    print STDERR "Unrecognized library type $libtype in $line\n";
	}
	$skipAction = 1;
    }
}

# ----------------------------------------------------------------------------
sub OutputLibLIBADD {
    my ($action, $lib, $libloc) = @_;

    if (defined($lib_addobjects{$lib})) {
	&LibraryAddObjects( $lib, $libloc );
    }
}

sub OutputShLibLIBADD {
    my ($action, $lib, $libloc) = @_;

    #print "Processing shared library $lib\n"; 
    # FIXME: we need to do something different for libtool libraries
    if (defined($liblt_addobjects{$lib})) {
	&LibraryLTAddObjects( $lib, $libloc );
    }

    if (defined($libso_addobjects{$lib})) {
	&LibrarySOAddObjects( $lib, $libloc );
    }
}

sub OutputLibDependenciesLIBADD {
    my ($action,$lib,$libloc,$memext) = @_;

    my $libset = "lib";
    if ($memext eq "o") { $libset .= "_addobjects"; }
    elsif ($memext eq "lo") { $libset .= "so_addobjects"; }

    if (defined($$libset{$lib})) {
	foreach $libin (split(/\s+/,$$libset{$lib})) {
	    if ($libin =~ /^-l/) { next; }
	    &print_make_line( FD, "$libin " );
	}
    }
}
# ----------------------------------------------------------------------------
# Add objects from a given library.  This version just handles standard
# libraries (.a), not libtool libraries (.la).
sub LibraryAddObjects {
    my ($lib, $libloc) = @_;

    print FD "\tmkdir .libtmp$newline";
    print FD "\trm -f .libtmp/*$newline";
    foreach $objlib (split(/\s+/,$lib_addobjects{$lib})) {
	# For static libraries, ignore any -l<name>
	if ($objlib =~ /^-l/) { next; }
	# If the added library is not an absolute path and is local,
	# add ../
	if ($objlib =~ /^[^\/\\].*lib*/) { $objlib = "../$objlib"; }
	elsif ($objlib =~ /^lib/) {$objlib = "../$objlib"; }
	print FD "\t(cd .libtmp \&\& \${AR} x $objlib)$newline";
    }
    print FD "\t\${AR} cru lib${lib}.a .libtmp/*$newline";
    print FD "\trm -rf .libtmp$newline";
}

# This is a stub for supporting libtool archives
sub LibraryLTAddObjects {
    my ($lib, $libloc) = @_;

    print FD "\tmkdir .libtmp$newline";
    print FD "\trm -f .libtmp/*$newline";
    foreach $objlib (split(/\s+/,$liblt_addobjects{$lib})) {
	# If the added library is not an absolute path and is local,
	# add ../
	if ($objlib =~ /^lib/) { $objlib = "../$objlib"; }
	print FD "\t(cd .libtmp \&\& \${LIBTOOL} \${AR} x $objlib)$newline";
    }
    print FD "\t\${LIBTOOL} \${AR} cru lib${lib}.a .libtmp/*$newline";
    print FD "\trm -rf .libtmp$newline";
}

# This is a stub for supporting libtool archives
sub LibrarySOAddObjects {
    my ($lib, $libloc) = @_;

    print FD "\tmkdir .libtmp$newline";
    print FD "\trm -f .libtmp/*$newline";
    foreach $objlib (split(/\s+/,$libso_addobjects{$lib})) {
	# For archives of shared objects, ignore any -l<name>
	if ($objlib =~ /^-l/) { next; }
	# If the added library is not an absolute path and is local,
	# add ../
	if ($objlib =~ /^lib/) { $objlib = "../$objlib"; }
	print FD "\t(cd .libtmp \&\& \${AR} x $objlib)$newline";
    }
    print FD "\t\${AR} cru lib${lib}.la .libtmp/*$newline";
    print FD "\trm -rf .libtmp$newline";
}

1;
